import axios from 'axios'
import { message } from 'ant-design-vue'

// 创建针对目标接口的axios实例
const apiUrl = window.g.baseURL;
console.log(apiUrl);
const adminApi = axios.create({
  baseURL: apiUrl,
  timeout: 15000,
  headers: { 'Content-Type': 'application/json' }
});

// 请求拦截器（可选）
adminApi.interceptors.request.use(config => {
  const token = localStorage.getItem('token');
  if (token) config.headers.token = `${token}`;
  return config;
});

// 响应拦截器（可选）
adminApi.interceptors.response.use(
  response => response.data,
  error => {
    message.error(error.response?.data?.message || '请求失败');
    return Promise.reject(error);
  }
);

// 导出封装的API方法
export default {
  admin: {
    login(data) {
      return adminApi.post('/api/download/admin/login', data);
    },
    fetchAppList(data) {
      return adminApi.post('/api/download/admin/info', data);
    },
    saveApp(data, token) {
      const url = token ? `/api/download/admin/save?token=${token}` : '/api/download/admin/save';
      return adminApi.post(url, data);
    },
  
    uploadApk(formData) {
      return adminApi.post('/api/download/admin/upload', formData, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      });
    },
    uploadImg(formData) {
      return adminApi.post('/wap/api/uploadimg!execute.action?lang=cn', formData, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      });
    }
    },
  
}
